<?php

defined('SYSPATH') or die('No direct script access.');

class Controller_Project2 extends Controller {

	public function after() {
		$this->response->body($this->_body . (Kohana::$profiling ? View::factory('profiler/stats') : ''));
		parent::after();
	}

	public function action_cian() {

		$html = $this->query('http://www.cian.ru/cat.php?deal_type=1&obl_id=1&city[0]=1', TRUE);
		//$html = $this->query('http://a.lidocs.ru/login', TRUE);
		$document = phpQuery::newDocument($html);
		$rows = pq("table.cat tr[id^='tr_']");
		foreach ($rows as $id => $row) {
			pq($row)->find("br")->replaceWith(" ");
			pq($row)->find("div.complaint")->replaceWith("");
			pq($row)->find("span.np")->replaceWith("");
			$data[$id]['distance'] = pq($row)->find("td[id$='_metro']")->clone()->children()->remove()->end()->text();
			$data[$id]['city'] = pq($row)->find("td[id$='_metro'] a")->eq(0)->text();
			$data[$id]['street'] = pq($row)->find("td[id$='_metro'] a")->eq(1)->text();
			$data[$id]['house'] = pq($row)->find("td[id$='_metro'] a")->eq(2)->text();
			$data[$id]['metro'] = pq($row)->find("td[id$='_metro'] a")->eq(3)->text();
			$data[$id]['room'] = pq($row)->find("td[id$='_room'] a")->text();
			$data[$id]['rooms'] = pq($row)->find("td[id$='_rooms']")->text();
			$data[$id]['price'] = pq($row)->find("td[id$='_price']")->text();
			$data[$id]['floor'] = pq($row)->find("td[id$='_floor']")->text();
			$data[$id]['dopsved'] = pq($row)->find("td[id$='_dopsved']")->text();
			$data[$id]['com'] = pq($row)->find("td[id$='_com']")->text();
			$data[$id]['contacts'] = pq($row)->find("td[id$='_contacts'] a")->text();
			$data[$id]['datetime'] = pq($row)->find("td[id$='_comment'] span[id^='added_']")->text();
			$data[$id]['comment'] = pq($row)->find("td[id$='_comment']")->clone()->children()->remove()->end()->text();
		}
		foreach ($data as $id => &$arr) {
			foreach ($arr as &$str) {
				$str = preg_replace('"\s+"si', ' ', trim(str_replace('&nbsp;', ' ', $str)));
			}
		}
		$this->_body = '<pre>' . print_r($data, TRUE);
	}

	public function action_irr() {
		$html = $this->query('http://irr.ru/real-estate/rent/search/list=list/page_len60/', TRUE);
		//exit(); //$html = $this->query('http://a.lidocs.ru/login', TRUE);
		$document = phpQuery::newDocument($html);
		$rows = pq("div.add_list.add_type4");
		foreach ($rows as $id => $row) {
			pq($row)->find("br")->replaceWith(" ");
			$data[$id]['datetime'] = pq($row)->find("p.adv_data")->eq(0)->text(); //.' - ';
			$data[$id]['comment'] = pq($row)->find("div.pic_wrap a img")->eq(0)->attr('alt'); //.' - ';
			$data[$id]['city'] = pq($row)->find("div.add_info div.main_props a.add_title")->eq(0)->text(); //.' - ';
			$data[$id]['metro'] = pq($row)->find("div.add_info div.main_props div.placed")->eq(0)->text(); //.' - ';
			$data[$id]['room'] = pq($row)->find("div.add_info div.main_props div.flat_prop div.flat_p")->eq(0)->text(); //.' - ';
			$data[$id]['square'] = pq($row)->find("div.add_info div.main_props div.flat_prop div.flat_p")->eq(1)->text(); //.' - ';
			$data[$id]['etage'] = pq($row)->find("div.add_info div.main_props div.flat_prop div.flat_p")->eq(2)->text(); //.' - ';
			$data[$id]['tag'] = pq($row)->find("div.add_info div.main_props div.tags")->eq(0)->text(); //.' - ';
			pq($row)->find("div.add_info div.right_block div.add_cost span.rubl")->replaceWith("руб ");
			$data[$id]['cost'] = pq($row)->find("div.add_info div.right_block div.add_cost")->eq(0)->text(); //.' - ';
			//echo $data[$id]['metro'] = pq($row)->find("div.add_info div.main_props div.placed")->eq(0)->text();
			//echo $data[$id]['metro'] = pq($row)->find("div.add_info div.main_props div.placed")->eq(0)->text();
			//echo $data[$id]['datetime'] = pq($row)->find("p.adv_data")->eq(0)->text();
			//echo $data[$id]['datetime'] = pq($row)->find("p.adv_data")->eq(0)->text();
			//echo '<br>'.PHP_EOL;




			/*
			  $data[$id]['distance'] = pq($row)->find("td[id$='_metro']")->clone()->children()->remove()->end()->text();
			  $data[$id]['city'] = pq($row)->find("td[id$='_metro'] a")->eq(0)->text();
			  $data[$id]['street'] = pq($row)->find("td[id$='_metro'] a")->eq(1)->text();
			  $data[$id]['house'] = pq($row)->find("td[id$='_metro'] a")->eq(2)->text();
			  $data[$id]['metro'] = pq($row)->find("td[id$='_metro'] a")->eq(3)->text();
			  $data[$id]['room'] = pq($row)->find("td[id$='_room'] a")->text();
			  $data[$id]['rooms'] = pq($row)->find("td[id$='_rooms']")->text();
			  $data[$id]['price'] = pq($row)->find("td[id$='_price']")->text();
			  $data[$id]['floor'] = pq($row)->find("td[id$='_floor']")->text();
			  $data[$id]['dopsved'] = pq($row)->find("td[id$='_dopsved']")->text();
			  $data[$id]['com'] = pq($row)->find("td[id$='_com']")->text();
			  $data[$id]['contacts'] = pq($row)->find("td[id$='_contacts'] a")->text();
			  $data[$id]['datetime'] = pq($row)->find("td[id$='_comment'] span[id^='added_']")->text();
			  $data[$id]['comment'] = pq($row)->find("td[id$='_comment']")->clone()->children()->remove()->end()->text();
			 *
			 */
		}
		foreach ($data as $id => &$arr) {
			foreach ($arr as &$str) {
				$str = preg_replace('"\s+"si', ' ', trim(str_replace('&nbsp;', ' ', $str)));
			}
		}
		$this->_body = '<pre>' . print_r($data, TRUE);
	}

	private function query($url, $cache = FALSE) {

		if (isset(Cache::$instances['redis'])) {
			$redis = Cache::$instances['redis'];
		}
		else {
			$redis = Cache::instance('redis');
		}
		$data = $redis->get(md5($url), FALSE);
		if (!empty($data) AND $cache == TRUE) {
			//echo 'cache';
			return $data;
		}
		else {
			$request = Request::factory($url)
					->method(Request::GET);
			$request->client()
					->options(array(
						CURLOPT_SSL_VERIFYPEER => FALSE,
						CURLOPT_USERAGENT => Request::$user_agent,
						CURLOPT_HTTPHEADER => array(
							'Accept-Encoding:gzip,deflate,sdch',
							'Accept-Language:ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4',
						),
						//CURLOPT_PROXY => '89.169.49.208:21320',
						CURLOPT_FOLLOWLOCATION => TRUE,
						CURLOPT_COOKIEFILE => APPPATH . "config/cookie.file",
						CURLOPT_COOKIEJAR => APPPATH . "config/cookie.file",
			));
			//echo 'query';
			$response = $request->execute();
			$data = $response->body();
			$redis->set(md5($url), $data, 3600);
		}
		return $data;
	}

	public function train() {

	}

	private function ImageToBlackAndWhite($im) {
		//imagefilter($im, IMG_FILTER_CONTRAST, -100);
		//imagefilter($im, IMG_FILTER_BRIGHTNESS, 50);
		for ($x = imagesx($im); $x--;) {
			for ($y = imagesy($im); $y--;) {
				$rgb = imagecolorat($im, $x, $y);
				$r = ($rgb >> 16) & 0xFF;
				$g = ($rgb >> 8 ) & 0xFF;
				$b = $rgb & 0xFF;
				$gray = ($r + $g + $b) / 3;
				if ($gray < 0xC0) {

					imagesetpixel($im, $x, $y, 0xFFFFFF);
				}
				else
					imagesetpixel($im, $x, $y, 0x000000);
			}
		}

		imagefilter($im, IMG_FILTER_NEGATE);

		// define the sharpen matrix
		$sharpen = array(
			array(0.0, -1.0, 0.0),
			array(-1.0, 5.0, -1.0),
			array(0.0, -1.0, 0.0)
		);

// calculate the sharpen divisor
		//$divisor = array_sum(array_map('array_sum', $sharpen));
// apply the matrix
		//imageconvolution($im, $sharpen, $divisor, 0);
	}

	private function getsymb($output) {
		$value = max($output);
		$key = array_search($value, $output);
		$str = '0123456789+-()';
		return $str[$key];
	}

	private function getans($answer) {
		// 0123456789+-()
		return $array = array(
			$answer == '0' ? 1 : 0,
			$answer == '1' ? 1 : 0,
			$answer == '2' ? 1 : 0,
			$answer == '3' ? 1 : 0,
			$answer == '4' ? 1 : 0,
			$answer == '5' ? 1 : 0,
			$answer == '6' ? 1 : 0,
			$answer == '7' ? 1 : 0,
			$answer == '8' ? 1 : 0,
			$answer == '9' ? 1 : 0,
			$answer == '+' ? 1 : 0,
			$answer == '-' ? 1 : 0,
			$answer == '(' ? 1 : 0,
			$answer == ')' ? 1 : 0,
		);
	}

	public function action_image() {
		//echo APPPATH.'files/train/+7(905)730-97-10.jpg';
		$image = imagecreatefromjpeg(DOCROOT . 'files/train/+7(905)730-97-10.jpg');
		$this->ImageToBlackAndWhite($image);
		$mask = $this->explode_symbols($image, 8, 15);
		print_r($mask);
		//header("Content-Type: image/jpeg");
		//imagegif($image);
		exit();
	}

	private function img_crop($src, $x, $y, $w, $h) {
		$dest = imagecreatetruecolor($w, $h);
		//echo $x.' - '.$y.' - '.$w.' - '.$h;
		// Копирование
		imagecopy($dest, $src, 0, 0, $x, $y, $w, $h);
		header("Content-Type: image/gif");
		imagegif($dest);
		imagedestroy($dest);
		//exit();
	}

	private function explode_symbols($image, $w, $h) {
		$count = floor((imagesx($image) - 2) / $w);
		for ($i = 0; $i < $count; $i++) {
			for ($x = 0 + $w * $i; $x < $w + $w * $i; $x++) {
				for ($y = 0; $y < $h; $y++) {
					$p = imagecolorat($image, $x, $y);
					$mask[$i][] = $p > 0 ? 0 : 1;
				}
			}
			//$this->img_crop($image, $x-8, $y-15, $w, $h);
		}
		return $mask;
	}

	public function action_train() {
		$num_input = 8 * 15;
		$num_output = 14; //count($this->getsymb(0));
		$num_layers = 3;
		$num_neurons_hidden = $num_input + $num_output;
		$desired_error = 0.003;
		$max_epochs = 1000000;
		$epochs_between_reports = 1000;

		$ann = fann_create_standard($num_layers, $num_input, $num_neurons_hidden, $num_output);
		//fann_set_activation_function_hidden($ann, FANN_SIGMOID_SYMMETRIC);
		//fann_set_activation_function_output($ann, FANN_SIGMOID_SYMMETRIC);
		//$ann = fann_create(array(360, 150, 10), 1, 0.7);
		//exit('123');
		$dir = DOCROOT . 'files/train/';
		$set = $sets = $answers = array();
		if ($dh = opendir($dir)) {
			while (($file = readdir($dh)) !== false) {
				if (filetype($dir . $file) == 'file') {
					$answer = str_replace('.jpg', '', $file); //.PHP_EOL;
					echo $answer . PHP_EOL;
					echo count(str_split($answer)) . PHP_EOL;
					$image = imagecreatefromjpeg($dir . $file);

					$this->ImageToBlackAndWhite($image);
					if (is_resource($image)) {
						$set = $this->explode_symbols($image, 8, 15);
						//print_r($set);
						if (!empty($set)) {
							//
							$sets = array_merge($sets, $set);
							$answers = array_merge($answers, str_split($answer));
							echo count(str_split($answer)) . ' - ' . count($set) . PHP_EOL;
							//if (count(str_split($answer)) != 5) {
							//	exit($answer);
							//}
							//print_r($sets);
							//exit();
							//fann_train($ann, $set, str_split($answer));
						}
					}
					else {
						echo $dir . $file . PHP_EOL;
					}
				}
			}
			closedir($dh);
		}
		echo fann_get_num_input($ann) . PHP_EOL;
		echo fann_get_num_output($ann) . PHP_EOL;
		//exit;
		echo count($sets) . PHP_EOL;
		echo count($answers) . PHP_EOL;

		echo count($sets[0]) . PHP_EOL;
		echo count($this->getans($answers[0])) . PHP_EOL;
		foreach ($answers as $id => $answer) {
			//echo count($sets[$id]);
			//exit();
			//$data[] = array($sets[$id], getans($answer));
			$ans[] = $this->getans($answer);
		}

		foreach ($ans as $id => $val) {
			if ($id == 0) {
				file_put_contents($dir . 'train.txt', count($sets) . ' ' . count($sets[$id]) . ' ' . count($val) . PHP_EOL);
			}
			file_put_contents($dir . 'train.txt', implode(' ', $sets[$id]) . PHP_EOL, FILE_APPEND);
			file_put_contents($dir . 'train.txt', implode(' ', $val) . PHP_EOL, FILE_APPEND);
		}
		//fann_train($ann, $sets, $ans);
		//fann_save($ann, 'fann2.data');
	}

//train();

	public function action_check() {
		echo date('Y.m.d H:i:s') . ' Start ' . PHP_EOL;
		$dir = DOCROOT . 'files/train/';
		$num_input = 8 * 15;
		$num_output = 14;
		$num_layers = 3;
		$num_neurons_hidden = $num_input + $num_output;
		$desired_error = 0.002;
		$max_epochs = 1000000;
		$epochs_between_reports = 1000;

		$ann = fann_create_standard($num_layers, $num_input, $num_neurons_hidden, $num_output);
		echo date('Y.m.d H:i:s') . ' Create FANN ' . PHP_EOL;
		if ($ann) {
			fann_set_activation_function_hidden($ann, FANN_SIGMOID_SYMMETRIC);
			fann_set_activation_function_output($ann, FANN_SIGMOID_SYMMETRIC);

			$filename = trim($dir . "train.txt");
			if (!file_exists($filename)) {
				echo $filename . ' not exists' . PHP_EOL;
			}
			echo date('Y.m.d H:i:s') . ' Read file "train" ' . PHP_EOL;
			if (fann_train_on_file($ann, $filename, $max_epochs, $epochs_between_reports, $desired_error)) {
				echo date('Y.m.d H:i:s') . ' FANN trained ' . PHP_EOL;
				fann_save($ann, $dir . "trainphone.data");
				echo date('Y.m.d H:i:s') . ' FANN saved ' . PHP_EOL;
			}
			else {
				echo 'error!!!' . PHP_EOL;
			}
			fann_destroy($ann);
		}
	}

	public function action_test() {
		$dir = DOCROOT . 'files/train/';
		$ann = fann_create_from_file($dir . "trainphone.data");
		if ($dh = opendir($dir)) {
			while (($file = readdir($dh)) !== false) {
				$info = pathinfo($file);
				if (filetype($dir . $file) == 'file' AND $info['extension'] == 'jpg') {
					$image = imagecreatefromjpeg($dir . $file);
					$this->ImageToBlackAndWhite($image);
					$set = $this->explode_symbols($image, 8, 15);
					echo '<br><img src="/files/train/' . $file . '"><br>';
					foreach ($set as $str) {
						$output = fann_run($ann, $str);
						echo $this->getsymb($output);
					}

				}
			}
			closedir($dh);
		}
	}

}

// End Project2
